﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using Capacitacion.Models;

namespace Capacitacion.Controllers
{
    public class RestaurantController : Controller
    {
        //
        // GET: /Restaurant/
        readonly RestaurantsContext _context = new RestaurantsContext();
        public ActionResult Index(string state)
        {
            ViewBag.MSG = "Filter by Country :";
            ViewBag.States = _context.Restaurants.Select(r => r.Address.State).Distinct();

            if (state == "")
            {
                var model =
                    _context.Restaurants
                        .OrderByDescending(r => r.Address.City);
                return View(model);
            }
            else
            {
                var model =
                    _context.Restaurants
                        .OrderByDescending(r => r.Address.City)
                        .Where(r => r.Address.State == state || (state == null));
                return View(model);
            }
        }

        public ActionResult Details(int id)
        {
            var model = _context.Restaurants.Single(r => r.Id == id);

            return View(model);
        }
        //
        // GET: /Restaurant/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Restaurant/Create

        [HttpPost]
        public ActionResult Create(Restaurant restaurant)
        {
            if (ModelState.IsValid)
            {
                _context.Restaurants.Add(restaurant);
                _context.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(restaurant);
        }
        //
        // GET: /Restaurant/Edit/5

        public ActionResult Edit(int id)
        {
            Restaurant person = _context.Restaurants.Find(id);
            return View(person);
        }

        //
        // POST: /Restaurant/Edit/5

        [HttpPost]
        public ActionResult Edit(Restaurant person)
        {
            if (ModelState.IsValid)
            {
                _context.Entry(person).State = EntityState.Modified;
                _context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(person);
        }
    }
}
